﻿using System;
using System.Collections.Generic;
using System.ServiceModel;
using GetQuestionSetDataWebApplication.GetQuestionSetDataDataSetTableAdapters;
using System.ServiceModel.Activation;
using QuestionSetLibrary;

namespace GetQuestionSetDataWebApplication
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "GetQuestionSetByAdminIdService" in code, svc and config file together.
    public class GetQuestionSetByAdminIdService : IGetQuestionSetByAdminIdService
    {
        private ManageQuesSetTableAdapter mqsta = new ManageQuesSetTableAdapter();
        private ManageQuestionTableAdapter mqta = new ManageQuestionTableAdapter();
        private QuestionNameTableAdapter qnta = new QuestionNameTableAdapter();
        private QuestionSetNameTableAdapter qsnta = new QuestionSetNameTableAdapter();
        private ManageDefaultAnswerTableAdapter mdata = new ManageDefaultAnswerTableAdapter();

        string IGetQuestionSetByAdminIdService.GetQuestionSet(string adminId, string questionSetId)
        {
            QuestionSet qs = new QuestionSet(qsnta.GetQuestionSetNameById(Convert.ToInt16(questionSetId))[0].QuestionSetName);
            return ToJson(qs);
        }
        public string ToJson(Object oj)
        {
            System.Web.Script.Serialization.JavaScriptSerializer oSerializer =
         new System.Web.Script.Serialization.JavaScriptSerializer();
            string sJSON = oSerializer.Serialize(oj);
            return sJSON;
        }
        string IGetQuestionSetByAdminIdService.GetAllQuestionSetByAdmin(string adminId)
        {
            List<QuestionSet> tempQuestionSetList = new List<QuestionSet>();

            //Get all the question sets which is owned by the admin
            var questionSetTable = mqsta.GetQuestionSetByAdminId(Convert.ToInt16(adminId));
            //Get all the questions in all question set
            for (int numberOfQuestionSet = 0; numberOfQuestionSet < questionSetTable.Count; numberOfQuestionSet++)
            {
                var questionSetNameTable = qsnta.GetQuestionSetNameById(questionSetTable[numberOfQuestionSet].QuestionSetId);
                //Question set >From database to object
                QuestionSet qs = new QuestionSet(questionSetNameTable[0].QuestionSetName);
                qs.setId(questionSetNameTable[0].Id);
                var getQuestionsInQuestionSetTable = mqta.GetQuestionIdByQuestionSetId(questionSetTable[numberOfQuestionSet].QuestionSetId);
                for (int numberOfQuestionInQuestionSet = 0; numberOfQuestionInQuestionSet < getQuestionsInQuestionSetTable.Count; numberOfQuestionInQuestionSet++)
                {
                    int questionId = getQuestionsInQuestionSetTable[numberOfQuestionInQuestionSet].QuestionId;
                    //Get all the default answers in question
                    var defaultAnswerTable = mdata.GetDefaultAnswerByQuestionId(questionId);
                    //Check the number of answer to see ......
                    if (defaultAnswerTable.Count == 1)
                    {
                        //It is a text question
                        Question q = new TextQuestion();
                        q.setId(questionId);
                        q.setQuestionName(qnta.GetQuestionNameById(questionId)[0].QuestionnName);
                        q.addAnswer("");
                        qs.addQuestion(q);
                    }
                    else
                    {
                        //It is a multiple choice question
                        MultipleChoiceQuestion q = new MultipleChoiceQuestion();
                        q.setQuestionName(qnta.GetQuestionNameById(questionId)[0].QuestionnName);
                        q.setId(questionId);
                        for (int numberOfAnswerInQuestion = 0; numberOfAnswerInQuestion < defaultAnswerTable.Count; numberOfAnswerInQuestion++)
                        {
                            q.addAnswer(defaultAnswerTable[numberOfAnswerInQuestion].Answer);
                        }
                        qs.addQuestion(q);
                    }
                }
                tempQuestionSetList.Add(qs);
            }
            return ToJson((Object)tempQuestionSetList.ToArray());
        }

        string IGetQuestionSetByAdminIdService.GetAllQuestionSet()
        {
            List<QuestionSet> tempQuestionSetList = new List<QuestionSet>();

            //Get all the question sets which is owned by the admin
            var questionSetTable = mqsta.GetData();
            //Get all the questions in all question set
            for (int numberOfQuestionSet = 0; numberOfQuestionSet < questionSetTable.Count; numberOfQuestionSet++)
            {
                var questionSetNameTable = qsnta.GetQuestionSetNameById(questionSetTable[numberOfQuestionSet].QuestionSetId);
                //Question set >From database to object
                QuestionSet qs = new QuestionSet(questionSetNameTable[0].QuestionSetName);
                qs.setId(questionSetNameTable[0].Id);
                var getQuestionsInQuestionSetTable = mqta.GetQuestionIdByQuestionSetId(questionSetTable[numberOfQuestionSet].QuestionSetId);
                for (int numberOfQuestionInQuestionSet = 0; numberOfQuestionInQuestionSet < getQuestionsInQuestionSetTable.Count; numberOfQuestionInQuestionSet++)
                {
                    int questionId = getQuestionsInQuestionSetTable[numberOfQuestionInQuestionSet].QuestionId;
                    //Get all the default answers in question
                    var defaultAnswerTable = mdata.GetDefaultAnswerByQuestionId(questionId);
                    //Check the number of answer to see ......
                    if (defaultAnswerTable.Count == 1)
                    {
                        //It is a text question
                        Question q = new TextQuestion();
                        q.setId(questionId);
                        q.setQuestionName(qnta.GetQuestionNameById(questionId)[0].QuestionnName);
                        q.addAnswer("");
                        qs.addQuestion(q);
                    }
                    else
                    {
                        //It is a multiple choice question
                        MultipleChoiceQuestion q = new MultipleChoiceQuestion();
                        q.setQuestionName(qnta.GetQuestionNameById(questionId)[0].QuestionnName);
                        q.setId(questionId);
                        for (int numberOfAnswerInQuestion = 0; numberOfAnswerInQuestion < defaultAnswerTable.Count; numberOfAnswerInQuestion++)
                        {
                            q.addAnswer(defaultAnswerTable[numberOfAnswerInQuestion].Answer);
                        }
                        qs.addQuestion(q);
                    }
                }
                tempQuestionSetList.Add(qs);
            }
            return ToJson((Object)tempQuestionSetList.ToArray());
        }
    }

}

